//
//  GroupMemberView.m
//  AAIM
//
//  Created by awh on 2019/12/15.
//  Copyright © 2019年 AAIM. All rights reserved.
//

#import "GroupMemberView.h"
#import "ContactModel.h"
@interface GroupMemberView()<UICollectionViewDelegate,UICollectionViewDataSource>
@property(nonatomic,strong)UICollectionView * memberCollectionView;
@property(nonatomic,strong)GroupMembersLayout * layout;
@property (nonatomic,strong)NSString * type;
@end

@implementation GroupMemberView
-(instancetype)initWithFrame:(CGRect)frame dataArray:(NSMutableArray*)dataArray withIsMaster:(nonnull NSString *)type
{
    if (self) {
        self=[super initWithFrame:frame];
        _DataArray=[[NSMutableArray alloc]init];
        UILabel * title=[[UILabel alloc]initWithFrame:CGRectMake(15, 15, 180, 18)];
        title.text=@"群成员";
        title.font=[UIFont systemFontOfSize:15];
        title.textColor=KHEXCOLOR(AllBackColor_titleColor);
        [self addSubview:title];
        _type = type;
        UIImageView * image=[[UIImageView alloc]initWithFrame:CGRectMake(self.width-30, title.y, 15, 15)];
        image.image=[UIImage imageNamed:@"群设置-返回键"];
        image.contentMode=UIViewContentModeScaleAspectFit;
        [self addSubview:image];
        self.DataArray=dataArray;
        UIButton * detailBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [detailBtn addTarget:self action:@selector(groupMemberDetail) forControlEvents:UIControlEventTouchUpInside];
        [detailBtn setBackgroundColor:[UIColor clearColor]];
        [detailBtn setFrame:CGRectMake(self.width-60, 15, 40, 30)];
        [self addSubview:detailBtn];
        CGFloat width = SCREEN_WIDTH;
        NSInteger col = 5; // 列数
        //y右上角用户表
        _layout = [[GroupMembersLayout alloc]init];
        if (self.DataArray.count>5) {
            _layout.sectionInset = UIEdgeInsetsMake(5,0, 2, 0);
        }else{
            _layout.sectionInset = UIEdgeInsetsMake(5,0,2, 0);
        }
        _layout.minimumInteritemSpacing = 1;
        _layout.minimumLineSpacing = 1;
        CGFloat itemWidth = (width - 1 * (col-1) - _layout.sectionInset.left - _layout.sectionInset.right) / col;
        if ([type isEqualToString:@"1"]) {
            if (self.DataArray.count>5) {
                      _layout.itemSize = CGSizeMake(itemWidth, 70);
                   }else{
                       _layout.itemSize = CGSizeMake(itemWidth, 40);
                   }
        }else{
        if (self.DataArray.count>5) {
           _layout.itemSize = CGSizeMake(itemWidth, 70);
        }else{
            _layout.itemSize = CGSizeMake(itemWidth, 40);
          }
        }
        _memberCollectionView=[[UICollectionView alloc]initWithFrame:CGRectMake(0 ,title.bottom+15, width, self.height-40) collectionViewLayout:_layout];
        _memberCollectionView.backgroundColor=[UIColor whiteColor];  _memberCollectionView.backgroundColor=[UIColor whiteColor];
        _memberCollectionView.pagingEnabled=YES;
        _memberCollectionView.dataSource=self;
        _memberCollectionView.delegate=self;
        _memberCollectionView.showsHorizontalScrollIndicator=NO;
        [_memberCollectionView registerClass:[GroupMembersCell class] forCellWithReuseIdentifier:@"cell"];
        [self addSubview:self.memberCollectionView];
        
        [self.memberCollectionView reloadData];
    }
    return self;
}
-(void)setDataWithArray:(NSMutableArray*)dataArray isHiden:(BOOL)isHiden
{
    //    [self.DataArray removeAllObjects];
    //    self.DataArray=dataArray;
    //    //重新布局
    //    CGFloat width = SCREEN_WIDTH;
    //    NSInteger col = 5; // 列数
    //    _layout = [[GroupMembersLayout alloc]init];
    //    if (self.DataArray.count>5) {
    //        self.memberCollectionView.frame=CGRectMake(0 ,40, width, self.height-40);
    //
    //        _layout.sectionInset = UIEdgeInsetsMake(7.5,0, 2, 0);
    //    }else
    //    {
    //        self.memberCollectionView.frame=CGRectMake(0 ,40, width, self.height-40);
    //
    //        _layout.sectionInset = UIEdgeInsetsMake(5,0, 0, 0);
    //    }
    //    _layout.minimumInteritemSpacing = 1;
    //    _layout.minimumLineSpacing = 1;
    //    CGFloat itemWidth = (width - 1 * (col-1) - _layout.sectionInset.left - _layout.sectionInset.right) / col;
    //    _layout.itemSize = CGSizeMake(itemWidth, 75);
    //    self.memberCollectionView.collectionViewLayout=_layout;
    //    self.memberCollectionView.frame=CGRectMake(0 ,40, width, self.height-40);
    //    [self.memberCollectionView reloadData];
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.DataArray.count;
}

- (void)groupMemberDetail{
    if ([self.delegate respondsToSelector:@selector(didMembersViewWithType:)]) {
        [self.delegate didMembersViewWithType:@"detailMember"];
    }
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    if ([_type isEqualToString:@"1"]) {
        if (self.DataArray.count-1==indexPath.row) {
            if ([self.delegate respondsToSelector:@selector(didMembersViewWithType:)]) {
                [self.delegate didMembersViewWithType:@"delete"];
            }
        }else if (self.DataArray.count-2==indexPath.row)
        {
            if ([self.delegate respondsToSelector:@selector(didMembersViewWithType:)]) {
                [self.delegate didMembersViewWithType:@"add"];
            }
        }
    }else{
        if (self.DataArray.count-1==indexPath.row) {
            if ([self.delegate respondsToSelector:@selector(didMembersViewWithType:)]) {
                [self.delegate didMembersViewWithType:@"add"];
            }
        }
    }
    
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(0*SCALAE, 0*SCALAE, 0*SCALAE, 0*SCALAE);
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    GroupMembersCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    //    cell.backgroundColor=[UIColor blueColor];
    ContactModel *model =self.DataArray[indexPath.row];
    [cell.headImage sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",userHeaderImage,model.head]] placeholderImage:kHeadGroupPlaceholderImage];
    cell.namelbl.text=model.remark;
    if ([_type isEqualToString:@"1"]) {
        if (self.DataArray.count-1==indexPath.row||self.DataArray.count-2==indexPath.row) {
            cell.headImage.image=[UIImage imageNamed:model.head];
        }
    }else{
        if (self.DataArray.count-1==indexPath.row) {
            cell.headImage.image=[UIImage imageNamed:model.head];
        }
    }
    cell.backgroundColor=[UIColor whiteColor];
    [collectionView deselectItemAtIndexPath:indexPath animated:NO];
    return cell;
}
@end
